﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

namespace BelleApp
{
    class DBManager
    {
        private const string CONNSTR = "data source=localhost; initial catalog=Supplier2Database;integrated security=true";

        public ArrayList SearchRFQ(int rfqID)
        {
            ArrayList RFQList = new ArrayList();
            RFQ rfq = new RFQ();
            SqlConnection conn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();

            try
            {
                //Get ConnectionString from database
                cmd.Connection = conn;
                conn.ConnectionString = CONNSTR;
                //Open connection
                conn.Open();
                //take in the values
                cmd.CommandText = "SELECT * FROM RFQ WHERE RFQID=@RFQID";
                cmd.Parameters.AddWithValue("@RFQID", rfqID);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    rfq.RFQID = Convert.ToInt32(dr[0].ToString());
                    rfq.CustomerID = Convert.ToInt32(dr[1].ToString());
                    rfq.RFQDate = dr[2].ToString();
                    rfq.Instructions = dr[3].ToString();
                    RFQList.Add(rfq);

                }
                dr.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();

            }

            return RFQList;
        }
        public ArrayList DisplayRFQ(int RFQID)
        {
            ArrayList list = new ArrayList();
            SqlConnection conn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            try
            {
                //Get ConnectionString from database

                cmd.Connection = conn;
                conn.ConnectionString = CONNSTR;
                //Open connection
                conn.Open();
                //take in the values
                cmd.CommandText = "SELECT RFQID,ItemID,Quantity,askingPrice,UnitPrice FROM RFQDetail r,Product p WHERE r.ItemID=p.ProductID AND r.RFQID=@RFQID";
                cmd.Parameters.AddWithValue("@RFQID", RFQID);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Quotation q = new Quotation();
                    q.RFQID = Convert.ToInt32(dr[0].ToString());
                    q.Name = dr[1].ToString();
                    q.Quantity = Convert.ToInt32(dr[2].ToString());
                    q.askingprice = Convert.ToDecimal(dr[3].ToString());
                    q.UnitPrice = Convert.ToDecimal(dr[4].ToString());
                    list.Add(q);


                }

                dr.Close();
            }
            catch (Exception ex)
            {
                throw ex;

            }
            finally
            {
            }
            return list;

        }

    }
}
